<template>
    <el-container style="height: 500px; border: 1px solid #eee">
        <el-aside width="200px" style="background-color: rgb(238, 241, 246)">
            <el-menu :default-openeds="['1', '3']">
                <el-submenu index="1">
                    <template slot="title"><i class="el-icon-message"></i>导航一</template>
                    <el-menu-item-group>
                        <template slot="title">分组一</template>
                        <el-menu-item index="1-1">选项1</el-menu-item>
                        <el-menu-item index="1-2">选项2</el-menu-item>
                    </el-menu-item-group>
                    <el-menu-item-group title="分组2">
                        <el-menu-item index="1-3">选项3</el-menu-item>
                    </el-menu-item-group>
                    <el-submenu index="1-4">
                        <template slot="title">选项4</template>
                        <el-menu-item index="1-4-1">选项4-1</el-menu-item>
                    </el-submenu>
                </el-submenu>
                <el-submenu index="2">
                    <template slot="title"><i class="el-icon-menu"></i>导航二</template>
                    <el-menu-item-group>
                        <template slot="title">分组一</template>
                        <el-menu-item index="2-1">选项1</el-menu-item>
                        <el-menu-item index="2-2">选项2</el-menu-item>
                    </el-menu-item-group>
                    <el-menu-item-group title="分组2">
                        <el-menu-item index="2-3">选项3</el-menu-item>
                    </el-menu-item-group>
                    <el-submenu index="2-4">
                        <template slot="title">选项4</template>
                        <el-menu-item index="2-4-1">选项4-1</el-menu-item>
                    </el-submenu>
                </el-submenu>
                <el-submenu index="3">
                    <template slot="title"><i class="el-icon-setting"></i>导航三</template>
                    <el-menu-item-group>
                        <template slot="title">分组一</template>
                        <el-menu-item index="3-1">选项1</el-menu-item>
                        <el-menu-item index="3-2">选项2</el-menu-item>
                    </el-menu-item-group>
                    <el-menu-item-group title="分组2">
                        <el-menu-item index="3-3">选项3</el-menu-item>
                    </el-menu-item-group>
                    <el-submenu index="3-4">
                        <template slot="title">选项4</template>
                        <el-menu-item index="3-4-1">选项4-1</el-menu-item>
                    </el-submenu>
                </el-submenu>
            </el-menu>
        </el-aside>

        <el-container>
            <el-header style="text-align: right; font-size: 12px">
                <el-dropdown>
                    <i class="el-icon-setting" style="margin-right: 15px"></i>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item>查看</el-dropdown-item>
                        <el-dropdown-item>新增</el-dropdown-item>
                        <el-dropdown-item>删除</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
                <span>王小虎</span>
            </el-header>

            <el-main>
                <div style="width: 100%;display: flex;">
                    <el-input suffix-icon="el-icon-search" style="margin-right: 5px" placeholder="请输入书名"></el-input>
                    <el-input suffix-icon="el-icon-search" style="margin-right: 5px" placeholder="请输入作者"></el-input>
                    <el-button icon="el-icon-search" type="success">搜索</el-button>
                    <el-button icon="el-icon-plus" type="primary" @click="showAdd">新增</el-button>
                    <el-button type="danger" icon="el-icon" :disabled="this.multipleSelection.length==0" @click="batchDel">批量删除</el-button>
                </div>
                <el-table
                        :data="books"
                         border
                         stripe
                        @selection-change="handleSelectionChange">
                    <el-table-column
                            type="selection"
                            width="55">
                    </el-table-column>

                    <el-table-column prop="title" label="书名" width="140">
                    </el-table-column>
                    <el-table-column prop="author" label="作者" width="120">
                    </el-table-column>
                    <el-table-column prop="price" label="价格">
                    </el-table-column>
                    <el-table-column label="出版时间" prop="pubTime">
                    </el-table-column>
                    <el-table-column label="图片">
                         <template #default="scope">
                             <el-image
                                     style="width: 100px; height: 100px"
                                     :src="scope.row.pic"
                                     :preview-src-list="[scope.row.pic]">
                             </el-image>
                         </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button
                                    size="mini"
                                    @click="handleEdit(scope.row)">编辑</el-button>
                            <el-button
                                    size="mini"
                                    type="danger"
                                    @click="handleDelete(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-dialog
                        title="新增图书"
                        :visible.sync="dialogVisible"
                        width="30%"
                        :before-close="handleClose">
                    <el-form :model="book" label-width="200px">
                         <el-form-item label="图书名称">
                             <el-input v-model="book.title"></el-input>
                         </el-form-item>
                        <el-form-item label="图书作者">
                            <el-input v-model="book.author"></el-input>
                        </el-form-item>
                        <el-form-item label="图书价格">
                            <el-input v-model="book.price"></el-input>
                        </el-form-item>
                        <el-form-item label="出版时间">
                                <span class="demonstration">默认</span>
                                <el-date-picker
                                        v-model="book.pubtime"
                                        type="date"
                                        placeholder="选择日期">
                                </el-date-picker>
                        </el-form-item>
                        <el-form-item label="图书封面">
                            <el-upload
                                    class="upload-demo"
                                    action="books/upload"
                                    :on-success="successImg"
                            >
                                <el-button size="small" type="primary">点击上传</el-button>

                            </el-upload>
                        </el-form-item>
                    </el-form>
                    <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addBook">确 定</el-button>
  </span>
                </el-dialog>
            </el-main>
        </el-container>
    </el-container>

</template>

<script>
    import request from "../api/api";

    export default {
        name: "Home",
        data() {
            const item = {
                date: '2016-05-02',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1518 弄'
            };
            return {
                dialogVisible:false,
                book:{},
                books:[],
                multipleSelection:[],
                tableData: Array(20).fill(item)
            }
        },
        methods:{

            //批量删除
            batchDel(){
                this.$confirm('永久删除'+this.multipleSelection.length+'条数据？, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids='?';
                    this.multipleSelection.forEach(item=>{
                        ids += 'ids='+ item.id+'&';
                    })
                    request.delete('books/'+ids).then(resp=>{
                        if(resp.code===200){
                            this.$message.success('删除成功！');
                            this.findBooks();
                        }else{
                            this.$message.error('删除失败！');
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //单删
            handleDelete(data){
                this.$confirm('永久删除该图书, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    request.delete('books/'+data.id).then(resp=>{
                        if(resp.code===200){
                            this.$message.success('删除成功！');
                            this.findBooks();
                        }else{
                            this.$message.error('删除失败！');
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            successImg(resp){
                  console.log(resp.data);
                  //获取后台返回的图片名称，保存到数据库
                  this.book.pic=resp.data;
            },
            addBook(){
                alert('add');
                console.log(this.book);
                request.post("books/",this.book).then(resp=>{
                     if(resp.code===200){
                         this.$message.success('新增成功！');
                         this.dialogVisible=false;
                         this.book={};
                         this.findBooks();
                     }else{
                         this.$message.error('新增失败!');
                     }
                })
            },
            showAdd(){
               this.dialogVisible=true;

            },
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {});
            },

            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            findBooks(){
               request.get('books/').then(resp=>{
                   if(resp){
                       console.log(resp);
                       this.books=resp;
                   }
               })
            },
        },
        mounted() {
            this.findBooks();
        }
    }
</script>

<style scoped>

</style>
